package com.wodan.zhongjinjicang.common.orm.spring;

import java.lang.reflect.Proxy;


import com.wodan.zhongjinjicang.common.orm.command.SqlCommandExecutorRegistry;
import com.wodan.zhongjinjicang.common.orm.utils.ExecTimeRecordCollection;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public class RdsCommonDaoProxyFactoryBean<T> extends MapperFactoryBean<T> implements ApplicationContextAware {
	/**
	 * sql执行者的registry
	 */
	
	/**
	 * applicationContext
	 */
	private ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
	private SqlCommandExecutorRegistry sqlCommandExecutorRegistry;
	
	//@Autowired
	private ExecTimeRecordCollection execTimeRecordCollection;
	
	public void setSqlCommandExecutorRegistry(SqlCommandExecutorRegistry sqlCommandExecutorRegistry) {
		this.sqlCommandExecutorRegistry = sqlCommandExecutorRegistry;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	@Override
	public T getObject() throws Exception {
		T mybatisMapperDelegate = super.getObject();
		RdsCommonDaoProxy<T> proxyImpl = new RdsCommonDaoProxy<T>(sqlCommandExecutorRegistry, super.getObjectType(),
				mybatisMapperDelegate);
		execTimeRecordCollection=applicationContext.getBean(ExecTimeRecordCollection.class);
		proxyImpl.setExecTimeRecordCollection(execTimeRecordCollection);
		
		return (T) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] { super.getObjectType() },
				proxyImpl);
	}
}
